<?php

namespace App\Controllers\v1;

use App\Models\UserLog;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Controllers\BaseController;
use App\Services\ActiveService;

class ActiveController extends BaseController
{
    public $allows = ['getTypes', 'getActiveList', 'getActiveDetail'];

    /**
     * 获取优惠类型列表信息
     */
    public function getTypes(Request $request)
    {
        return ActiveService::getTypes();
    }

    /**
     * 申请参加活动
     */
    public function postActive(Request $request)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $params = $request->all();
        if ($active = ActiveService::checkActiveStatus($params['id'])) {
            $user = Auth::user();
            $updateParams = [
                "user_id" => $user->id,
                "username" => $user->username,
                "active_id" => $params['id'],
                "active_name" => $active['name'],
                "content" => isset($params['content']) ? $params['content'] : "",
            ];
            ActiveService::addApply($updateParams);
            return "true";
        } else {
            error_exit(SysCode::ACTIVE_STATUS_ERROR);
        }
    }

    /**
     * 活动详情
     */
    public function getActiveDetail(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required',
        ]);
        $source = isset($params['source']) ? $params['source'] : 'h5';//来源(1=h5,其他都表示pc)
        $select = [
            "active.id",
            "active.title",
            "active.cover",//活动封面图片
            "active.content_" . $source,
            "active.description",//活动描述
        ];
        $activeList = ActiveService::getActive($params, $select);
        if ($activeList) {
            return current($activeList);
        }
    }

    /**
     * 获取优惠信息列表
     */
    public function getActiveList(Request $request)
    {
        $params = $request->all();
        $select = [
            "active.id",
            "active.title",
            "active.cover",//活动封面图片
            "active.description",//活动描述
        ];
        $activeList = ActiveService::getActive($params, $select);
        return $activeList;
    }
}
